var merchantInfo;//用户信息列表
var merchantId = $("#merchantId").val();
initMerchantInfo();

function initMerchantInfo() {
    if (cochoApp.isNotEmpty(merchantId)) {
        getMerchantInfo();
        if (cochoApp.isNotEmpty(merchantInfo)) {
            $("#merchantName").html(merchantInfo.merchantName);
            $("#jszc").html(merchantInfo.merchantName);
        }
    }
}

function getMerchantInfo() {
    $.ajax({
        type: "post",
        async: false,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/anonymous/phone/getMerchantInfoById.do?token=" + cochoApp.getTokenByStorage() + "&merchantId=" + merchantId,
        dataType: "json",
        data: "",
        success: function (data) {
            if (data != null && data.statusCode == cochoApp.SUCCESS) {
                if (data.dataRows.length > 0) {
                    merchantInfo = data.dataRows[0];
                } else {
                    cochoAlert.notificationsWarn("暂无商户信息！");
                }
            } else {
                cochoAlert.notificationsWarn("获取商户信息失败！");
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        }
    });
}

function pay() {
    var chcdPara = $("#chcd").val();//付款方式
    if(chcdPara == "WXP"){

    }else if(chcdPara == "ALP"){

    }else{
        alert("不是合法的支付渠道，请重试！");
        return false;
    }

    var txamtPara = $("#txamtPara").val();//钱数
    var mchntidPara = merchantInfo.mchntid;//商户号
    if (txamtPara == null || txamtPara == "") {
        alert("对不起，请输入对应金额");
        $("#txamtPara").focus();
        return;
    }
    var n = Number(txamtPara);
    if (isNaN(n))//"非数字"
    {
        alert("对不起，请输入数字");
        return;
    }
    var num2 = n.toFixed(3);
    var num3 = num2.substring(0, num2.lastIndexOf('.') + 3);
    var num4 = Math.round((num3 * 100) * 1000) / 1000;//数据转换
    var price = (Array(12).join(0) + num4).slice(-12);//金额转换

    var backUrl = "http://122.114.10.50/anonymous/pay/callBackUrl.do";
    //支付类型
    var busicd = "WPAY";
    //编码格式
    var charset = "utf-8";
    //支付类型
    var chcd = chcdPara;

    var frontUrl = "http://122.114.10.50/control/phone/customerResult.jsp";
    //商户号
    var mchntid = mchntidPara;

    var orderNum = getNowFormatDate();
    //签名方式
    var signType = "SHA256";
    //终端号
    var terminalid = $("#terminalid").val();
    //交易金额
    var txamt = price;  //钱数
    //版本号
    var version = "2.1";
    var newString = "backUrl="+backUrl+"&busicd="+busicd+"&charset="+charset+"&chcd="+chcd+"&frontUrl="+frontUrl+"&mchntid="+mchntid+"&orderNum="+orderNum+""
        + "&signType="+signType+"&terminalid="+terminalid+"&txamt="+txamt+"&version="+version+"";

    var newSign = newString + merchantInfo.signKey;

    var sign = sha256_digest(newSign);

    var jsonString = {"orderNum":orderNum,"txamt":txamt,"busicd":busicd,"version":version,"chcd":chcd,"backUrl":backUrl,"frontUrl":frontUrl,"mchntid":mchntid,"charset":charset,"signType":signType,"terminalid":terminalid,"sign":sign};

    var jStr = "{ ";
    for(var item in jsonString){
        jStr += "\""+item+"\":\""+jsonString[item]+"\",";
    }
    var newJson=jStr.substring(0,jStr.length-1);
    newJson += " }";

    var base = new Base64();
    var result = base.encode(newJson);

    var paramData = {"paymentType": busicd, "merchantId": merchantId,"busicd":busicd,"origOrderNum":orderNum,"chcd":chcd,"txamt":price,"sign":sign,"terminalid":terminalid,"token": cochoApp.getTokenByStorage()};
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/anonymous/phone/toH5.do",
        dataType: "json",
        data: paramData,
        success: function (data) {
            if (data != null) {
                if (data.statusCode == "SUCCESS") {
                    window.location = "http://test.quick.ipay.so/scanpay/unified?data="+result+"";
                } else {
                    alert(data.statusMsg);
                    return false;
                }
            } else {
                alert("操作失败,请联系管理员解决！");
                return false;
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn("请求服务器出错！");
            return false;
        }
    });
}

function getNowFormatDate() {
    var date = new Date();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + month + strDate
        + date.getHours()  + date.getMinutes() + date.getSeconds() + date.getMilliseconds();
    return currentdate;
}
